package responseutil

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

const (
	SuccessCode int = 0
	ErrorCode   int = -1
)


type Response struct {
	// 代码
	Code int `json:"code" example:"200"`
	// 数据集
	Data interface{} `json:"data"`
	// 消息
	Msg string `json:"msg"`
}

var response *Response

func FailJson(c *gin.Context,msg string, data ...interface{}) {
	responseData := interface{}(nil)
	if len(data) > 0 {
		responseData = data[0]
	}
	response = &Response{
		Code: ErrorCode,
		Msg:  msg,
		Data: responseData,
	}
	c.JSON(http.StatusOK,response)
	return
}

func AbortFailJson(c *gin.Context,msg string, data ...interface{}) {
	responseData := interface{}(nil)
	if len(data) > 0 {
		responseData = data[0]
	}
	response = &Response{
		Code: ErrorCode,
		Msg:  msg,
		Data: responseData,
	}
	c.AbortWithStatusJSON(http.StatusOK,response)
	return
}

func SusJson(c *gin.Context,msg string, data ...interface{}) {
	responseData := interface{}(nil)
	if len(data) > 0 {
		responseData = data[0]
	}
	response = &Response{
		Code: SuccessCode,
		Msg:  msg,
		Data: responseData,
	}
	c.JSON(http.StatusOK,response)
	return
}
